import json
import time
import traceback

from django.core.paginator import Paginator
from django.shortcuts import render
# Create your views here.
from django.utils.decorators import method_decorator
from rest_framework.response import Response
from rest_framework.views import APIView

from logsManage import models
from logsManage.seriallizers import LogListSeriallizer
from tools.authToken import create_token, userAuth
from tools.encryption import getSha256
from tools.log import logger
from tools.tool import get_ip, get_json_values, get_file_type, get_time_uid, get_uuid, delete_file


# 日志列表
class LogList(APIView):

    @method_decorator(userAuth)
    def get(self, request, *args, **kwargs):
        li = []
        resp = {"code": 0, "data": li}
        try:
            ip = get_ip(request)
            body = request.query_params.dict()

            roomtypes = models.LogsList.objects.filter().order_by("timer")

            page_size = get_json_values("page_size", body, 10)
            page_num = get_json_values("page_num", body, 1)
            paginator = Paginator(roomtypes, page_size)
            roomtypes = paginator.get_page(page_num)
            total = paginator.count

            seriallizer = LogListSeriallizer(roomtypes, many=True)
            resp = {"code": 1, "data": seriallizer.data, 'total': total}
        except Exception as e:
            print(traceback.format_exc())
            # logger.error(traceback.format_exc())
        return Response(resp)


def set_server_logs(name, grade, describe, user_id, ip):

    models.LogsList.objects.create(
        name=name,
        grade=grade,
        describe=describe,
        user_id=user_id,
        ip=ip,
    )
